package vitaliy.bondar.diamondcave;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public abstract class AnimatedCellEntity extends AnimatedSprite implements Constants, ICellEntity
{
    protected int m_cellX;
    protected int m_cellY;
    protected float m_moveDuration = 0.2f;
    protected boolean m_isMoving = false;

    public AnimatedCellEntity(final int cellX, final int cellY, final int width,
            final int height, final ITiledTextureRegion region,
            final VertexBufferObjectManager vbom)
    {
        super(cellX * CELL_WIDTH, cellY * CELL_HEIGHT, width, height, region, vbom);
        this.m_cellX = cellX;
        this.m_cellY = cellY;
    }

    public final int cellX()
    {
        return m_cellX;
    }

    public final int cellY()
    {
        return m_cellY;
    }

    public void setCell(final int pCellX, final int pCellY)
    {
        MoveModifier modifier = new MoveModifier(m_moveDuration, getX(), pCellX * CELL_WIDTH,
                getY(), pCellY * CELL_HEIGHT)
        {
            @Override
            protected void onModifierStarted(IEntity pItem)
            {
                super.onModifierFinished(pItem);
            }

            @Override
            protected void onModifierFinished(IEntity pItem)
            {
                super.onModifierFinished(pItem);
                setPosition(pCellX * CELL_WIDTH, pCellY * CELL_HEIGHT);
                m_isMoving = false;
            }
        };
        this.registerEntityModifier(modifier);
        this.m_cellX = pCellX;
        this.m_cellY = pCellY;
    }

    @Override
    public boolean isInSameCell(final ICellEntity pCellEntity)
    {
        return this.m_cellX == pCellEntity.cellX() && this.m_cellY == pCellEntity.cellY();
    }
}
